import { getDataPlnel, getTable, getDetail, getParticipants, getDocuments, exportDocument, downloadDocument, deleteDocument } from '@/api/cfManage'
import { Message } from 'element-ui'

const oldState = { // 用于重置state数据
  tableData: [
    // { id: 1, number: '6546545', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    // { id: 2, number: '65465456456455645644546546', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    // { id: 3, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    // { id: 4, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    // { id: 5, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    // { id: 6, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 },
    // { id: 7, number: '654654564564', time: '2020-09-21 09:23:08', name: '福讯会议', people: '刘有清', leader: '张三', type: '快速', peopleNum: 54, password: '123456', record: '有', file: 1 }

  ],
  panelData: [
    { id: 0, number: 123456, type: 'today_p', desc: '今日会议' },
    { id: 1, number: 123456, type: 'mouthAdd_p', desc: '本月会议' },
    { id: 2, number: 123456, type: 'meetingTotal', desc: '累计会议' }

  ],
  params: {
    page: 0,
    size: 10,
    cfCode: null,
    cfName: null,
    sponsorName: null,
    startTime1: null,
    startTime2: null
  },
  total: 0
}

const state = JSON.parse(JSON.stringify(oldState))

const mutations = {
  set_tableData (state, data) {
    state.tableData = data
  },
  set_panelData (state, data) {
    state.panelData[0].number = data.todayCount
    state.panelData[1].number = data.monthCount
    state.panelData[2].number = data.total
  },
  set_page (state, data) {
    state.params.page = data - 1
  },
  set_size (state, data) {
    state.params.size = data
  },
  set_cfCode (state, data) {
    state.params.cfCode = data
  },
  set_cfName (state, data) {
    state.params.cfName = data
  },
  set_sponsorName (state, data) {
    state.params.sponsorName = data
  },
  set_startTime1 (state, data) {
    state.params.startTime1 = data
  },
  set_startTime2 (state, data) {
    state.params.startTime2 = data
  },
  set_total (state, data) {
    state.total = data
  }

}
const actions = {
  getDataPlnelData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getDataPlnel(payload).then(res => {
        // // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_panelData', res.data.data)
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  getTableData ({ state, commit }) {
    return new Promise((resolve, reject) => {
      getTable(state.params).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          commit('set_total', res.data.data.size)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
          commit('set_total', 0)
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 获取详细信息
  getDetailData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getDetail(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 获取参会人信息
  getParticipantsData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getParticipants(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 获取参会文档信息
  getDocumentsData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      getDocuments(payload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          commit('set_tableData', res.data.data.list)
          resolve(res.data)
        } else if (res.data.code === 666 && res.data) {
          commit('set_tableData', [])
        } else {
          reject(res.data.message)
        }
      })
    })
  },
  // 导出会议文档
  exportDocumentData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      exportDocument(payload).then(res => {
        // console.log(res.data)
        // console.log(res, '导出会议文档')
        // resolve(res)
        if (res.data.type === 'application/json') {
          const reader = new FileReader()
          reader.readAsText(res.data, 'utf-8')
          reader.onload = () => {
            const newRes = JSON.parse(reader.result)
            // console.log(newRes)
            Message({
              type: 'error',
              message: newRes.message,
              duration: 1000
            })
          }
        } else {
          resolve(res)
        }
      })
    })
  },
  // 下载文档
  downloadDocumentData ({ state, commit }, payload) {
    return new Promise((resolve, reject) => {
      downloadDocument(payload).then(res => {
        // console.log(res.data)
        // resolve(res)
        // if (res.data.code === 200 && res.data) {
        // } else {
        //   reject(res.data.message)
        // }
        if (res.data.type === 'application/json') {
          const reader = new FileReader()
          reader.readAsText(res.data, 'utf-8')
          reader.onload = () => {
            const newRes = JSON.parse(reader.result)
            // console.log(newRes)
            Message({
              type: 'error',
              message: newRes.message,
              duration: 1000
            })
          }
        } else {
          // 正确回调函数
          resolve(res)
        }
      })
    })
  },
  // 删除文档
  deleteDocumentData ({ state, commit }, playload) {
    return new Promise((resolve, reject) => {
      deleteDocument(playload).then(res => {
        // console.log(res)
        if (res.data.code === 200 && res.data) {
          resolve(res.data)
        } else {
          reject(res.data.message)
        }
      })
    })
  }

}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
